package com.e3.web.widget.client;

import java.util.List;

import com.e3.core.lang.DataConstants;
import com.e3.web.core.client.CommonMessages;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.google.gwt.i18n.client.DateTimeFormat;

public class MyFormFieldFactory {

	public static Field<?> createDateField(String name, String dispName,boolean isNotNull) {
		MyDateField field = new MyDateField();
		DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-dd");
		field.getPropertyEditor().setFormat(format);

		field.setEmptyText(CommonMessages.INSTANCE.info_empty_format("2010-08-30"));
		field.setName(name);
		field.setFieldLabel(dispName);
		
		setFieldNotNull(field,isNotNull);
		
		return field;
	}
	
	public static Field<?> createDateTimeField(String name, String dispName,boolean isNotNull) {
		MyDateField field = new MyDateField();
		DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
		field.getPropertyEditor().setFormat(format);

		field.setName(name);
		field.setFieldLabel(dispName);
		field.setEmptyText(CommonMessages.INSTANCE.info_empty_format("2010-08-30 20:18:09"));
		setFieldNotNull(field,isNotNull);
		return field;
	}

	public static MyNumberField<Integer> createIntField(String name, String dispName,int length,boolean isNotNull) {
		MyNumberField<Integer> field = new MyNumberField<Integer>();
		field.setPropertyEditorType(Integer.class);
		field.setName(name);
		field.setFieldLabel(dispName);
		field.setMaxLength(length);
		setFieldNotNull(field,isNotNull);
		return field;
	}

	public static MyTextField<String> createStringField(String name, String dispName,int length,boolean isNotNull) {
		MyTextField<String> field = new MyTextField<String>();
		field.setName(name);
		field.setFieldLabel(dispName);
		field.setMaxLength(length);
		setFieldNotNull(field,isNotNull);
		return field;
	}

	public static MyTextField<String> createPasswordField(String name, String dispName,int length,boolean isNotNull) {
		MyTextField<String> field = new MyTextField<String>(true);
		field.setName(name);
		field.setFieldLabel(dispName);
		field.setMaxLength(length);
		
		setFieldNotNull(field,isNotNull);
		return field;
	}

	public static MyTextArea createTextAreaField(String name, String dispName,int length,boolean isNotNull) {
		MyTextArea field = new MyTextArea();
		field.setName(name);
		field.setFieldLabel(dispName);
		field.setMaxLength(length);
		setFieldNotNull(field,isNotNull);
		return field;
	}

	public static MyNumberField<Double> createDoubleField(String name, String dispName,int digit,int length,boolean isNotNull) {
		MyNumberField<Double> field = new MyNumberField<Double>();
		field.setPropertyEditorType(Double.class);
		field.setName(name);
		field.setFieldLabel(dispName);
		field.setMaxLength(length);
		setFieldNotNull(field,isNotNull);
		return field;
	}

	public static MyNumberField<Long> createLongField(String name, String dispName,int length,boolean isNotNull) {
		MyNumberField<Long> field = new MyNumberField<Long>();
		field.setPropertyEditorType(Long.class);
		field.setName(name);
		field.setFieldLabel(dispName);
		field.setMaxLength(length);
		setFieldNotNull(field,isNotNull);
		return field;
	}
	
	public static MyNumberField<Long> createIdField(String name) {
		MyNumberField<Long> field = new MyNumberField<Long>();
		field.setPropertyEditorType(Long.class);
		field.setName(name);
		field.setMaxLength(19);
		field.hide();
		return field;
	}

	public static MyCheckBox createCheckBooleanField(String name, String dispName) {
		MyCheckBox field = new MyCheckBox();
		field.setName(name);
		field.setFieldLabel(dispName);
		return field;
	}

	public static <X> MyRadioGroup<X> createRadioGroupField(String name, String dispName) {
		MyRadioGroup<X> radioGroup = new MyRadioGroup<X>(name);
		radioGroup.setName(name);
		radioGroup.setFieldLabel(dispName);
		return radioGroup;
	}

	public static <D> MyRefTextField<D> createRefField(String name, String dispName, MyRefDialog<D> refDialog,boolean isNotNull) {
		MyRefTextField<D> field = new MyRefTextField<D>();
		field.setRefDialog(refDialog);
		field.setName(name);
		field.setFieldLabel(dispName);
		setFieldNotNull(field,isNotNull);
		return field;
	}

	public static <X> MyListField<X> createListField(String name, String dispName, List<ListTypeModel<X>> listValueModels, boolean isForceSelected,boolean isNotNull) {
		MyListField<X> sfield = new MyListField<X>();
		sfield.setListValues(listValueModels);
		sfield.setForceSelection(isForceSelected);
		sfield.setName(name);
		sfield.setFieldLabel(dispName);
		setFieldNotNull(sfield,isNotNull);
		return sfield;
	}

	/**
	 * default value is 'yes' or 'no'.
	 * @param name
	 * @param dispName
	 * @param defaultValue
	 * @return
	 */
	public static <X> MyRadioGroup<X> createYesNoRadioGroupField(String name, String dispName,String defaultValue){
		MyRadioGroup<X> radioGroup = createRadioGroupField(name,dispName);
		boolean yesSelected = false;
		if("yes".equals(defaultValue)){
			yesSelected = true;
			
		}
		radioGroup.addRadio(CommonMessages.INSTANCE.lab_yes(), null, yesSelected);
		radioGroup.addRadio(CommonMessages.INSTANCE.lab_no(), null, !yesSelected);
		return radioGroup;
	}

	/**
	 * default value is 'yes' or 'no'.
	 * @param name
	 * @param dispName
	 * @param defaultValue
	 * @return
	 */
	public static MyRadioGroup<Integer> createGenderRadioGroupField(String name, String dispName){
		MyRadioGroup<Integer> radioGroup = createRadioGroupField(name,dispName);
		radioGroup.addRadio(CommonMessages.INSTANCE.lab_male(), DataConstants.MALE, true);
		radioGroup.addRadio(CommonMessages.INSTANCE.lab_female(), DataConstants.FEMALE, false);
		return radioGroup;
	}
	
	public static void setFieldNotNull(Field<?> field,boolean isNotNull){
		if(isNotNull){
			field.setLabelStyle("color:red;");
		}
	}

}
